package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asktgapp.R;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.TestApiUtil;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.PayFinishEvent;
import com.asktgapp.eventbus.PutQuestionsChooseEvent;
import com.asktgapp.model.PutQuestionsChooseVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//提问选择型号
public class PutQuestionsChooseModelFragment extends BaseFragment {


    private BaseAdapter mAdapter;
    private PutQuestionsChooseVO putQuestionsChooseVO;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_put_questions_type, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }


    public static PutQuestionsChooseModelFragment newInstance(PutQuestionsChooseVO data) {
        PutQuestionsChooseModelFragment fragment = new PutQuestionsChooseModelFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", data);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        Bundle bundle = getArguments();
        putQuestionsChooseVO = (PutQuestionsChooseVO) bundle.getSerializable("data");

        RecyclerView recyclerView = rootView.getRootView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_put_questions_cohoose_list, parent, false), new BaseViewHolder.Callbacks() {
                    @Override
                    public void bind(BaseViewHolder holder, Object data) {
                        if (data instanceof PutQuestionsChooseVO) {
                            PutQuestionsChooseVO bean = (PutQuestionsChooseVO) data;
                            holder.setText(R.id.money, bean.getSeriesName());
                            RecyclerView itemRv = holder.getView(R.id.itemRv);
                            itemRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
                            BaseAdapter mItemAdapter = new BaseAdapter(getActivity()) {
                                @Override
                                public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                                    BaseViewHolder holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_put_questions_cohoose, parent, false), new BaseViewHolder.Callbacks() {
                                        @Override
                                        public void bind(BaseViewHolder holder, Object data) {
                                            if (data instanceof PutQuestionsChooseVO.ModelsBean) {
                                                PutQuestionsChooseVO.ModelsBean bean = (PutQuestionsChooseVO.ModelsBean) data;
                                                holder.setText(R.id.content, bean.getModelName());
                                            }

                                        }

                                        @Override
                                        public void onItemClick(View view, int position) {
                                            PutQuestionsChooseVO.ModelsBean bean = (PutQuestionsChooseVO.ModelsBean) getData().get(position);
                                            putQuestionsChooseVO.setModelId(bean.getIdX());
                                            putQuestionsChooseVO.setModelName(bean.getModelName());
                                            putQuestionsChooseVO.setSeriesId(bean.getSeriesId());
                                            PutQuestionsChooseEvent.postEvent(putQuestionsChooseVO);
                                            PayFinishEvent.postEvent(true);
                                            getBaseActivity().onBackPressed();
                                        }
                                    });
                                    return holder;
                                }
                            };
                            itemRv.setAdapter(mItemAdapter);
                            mItemAdapter.setData(bean.getModels());
                        }

                    }

                    @Override
                    public void onItemClick(View view, int position) {
//                        PutQuestionsChooseVO bean = (PutQuestionsChooseVO) mAdapter.getData().get(position);
//                        Intent intent = new Intent(getActivity(), PutQuestionsChooseModelActivity.class);
//                        putQuestionsChooseVO.setBrandId(bean.getId());
//                        putQuestionsChooseVO.setBrandName(bean.getBrandName());
//                        putQuestionsChooseVO.setBrandSort(bean.getBrandSort());
//                        intent.putExtra("data", bean);
//                        startActivity(intent);
//                        getBaseActivity().onBackPressed();
                    }
                });
                return holder;
            }
        };

        recyclerView.setAdapter(mAdapter);

    }


    @Override
    public void loadData() {

        getBrandLlist();
    }

    private void getBrandLlist() {
        Apiservice apiservice = TestApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("brandId", putQuestionsChooseVO.getBrandId());
        Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call = apiservice.GetSeriesAndModelByBrand(params);
        call.enqueue(new Callback<ApiResponseBody<List<PutQuestionsChooseVO>>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Response<ApiResponseBody<List<PutQuestionsChooseVO>>> response) {
                if (response.isSuccessful()) {
                    List<PutQuestionsChooseVO> list = response.body().getResult();
                    if (list.size() == 0) {
                        visibleNoData();
                        dismissProgress();
                    } else
                        mAdapter.setData(list);
                } else {
                    showTost(response.raw().message(), 1);
                    dismissProgress();
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<List<PutQuestionsChooseVO>>> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                showTost(apiException.getMessage(), 1);

            }
        });


    }


}
